﻿// Dictionary.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "Dictionary.h"


#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	AddDictProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	DelAddProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	HelpProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	PrintProc(HWND, UINT, WPARAM, LPARAM);

MyComboBox cInput, cChoose;

MyStatic sInput;

MyButton bSearch, bHelp, bPrint, bPronounce, bEditW, bEditD;

MyGroupBox gOption, gSearch, gList, gMean, gChoose, gEdit;

MyListBox lList;

MyRichEditBox eMean;

MyHelp mHelp;

Mean mMean;

Dict mDict;

Word mWord;

//test remove
DBProc dbTest;

int APIENTRY _tWinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPTSTR    lpCmdLine,
	int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_DICTIONARY, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_DICTIONARY));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}


ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_DICTIONARY));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_DICTIONARY);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}



BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd;

	hInst = hInstance; // Store instance handle in our global variable

	hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME,
		CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);


	if (!hWnd)
	{
		return FALSE;
	}

	ShowWindow(hWnd, SW_MAXIMIZE);
	UpdateWindow(hWnd);
		
	return TRUE;
}
//Define dict
void DefineDict(MyComboBox mC, LPTSTR dictName, LPTSTR dictType)
{
	TCHAR buffer[100];
	LPTSTR name,type;
	wcscpy(buffer,mC.getText());
	type = wcstok(buffer,L"(");
	name = wcstok(NULL,L")");
	wcscpy(dictType,type);
	wcscpy(dictName,name);
	
}
//use for list
void RefreshList(MyListBox hList, _bstr_t low)
{

	LPTSTR aW;
	aW = wcstok(low,L"\n");
	hList.addItem(aW);
	do{
		aW = wcstok(NULL,L"\n");
		if (aW==NULL)break;
		hList.addItem(aW);
	}while (aW!=NULL);
	
}

//use for combobox
void RefreshDict(MyComboBox hDict, _bstr_t lod)
{
	
	LPTSTR aW;
	aW = wcstok(lod,L"\n");
	hDict.add(aW);
	hDict.setCurSel(0);
	do{
		aW = wcstok(NULL,L"\n");
		
		if (aW==NULL)break;
		hDict.add(aW);
	}while (aW!=NULL);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	RECT r;
	GetClientRect(hWnd,&r);

	switch (message)
	{

	
	case WM_CREATE:
		{
			
			//create object
			gOption.createGroupBox(L"Tuỳ Chọn",BS_LEFT,r.left + 330,r.top,r.right - 340,100,hWnd,NULL,
				18,FW_BOLD,FALSE,FALSE,L"Tahoma",GetWindowInstance(hWnd));

			gSearch.createGroupBox(L"Tra Từ",BS_LEFT,r.left + 10,r.top,r.left + 300,100,hWnd,NULL,
				18,FW_BOLD,FALSE,FALSE,L"Tahoma",GetWindowInstance(hWnd));

			gList.createGroupBox(L"Danh Sách Từ",BS_CENTER,r.left + 10,r.top + 110,r.left + 300,r.bottom - 120,hWnd,NULL,
				18,FW_BOLD,FALSE,FALSE,L"Tahoma",GetWindowInstance(hWnd));

			gMean.createGroupBox(L"Nghĩa Của Từ",BS_CENTER,r.left + 330,r.top + 110,r.right - 340,r.bottom - 120,hWnd,NULL,
				18,FW_BOLD,FALSE,FALSE,L"Tahoma",GetWindowInstance(hWnd));

			gChoose.createGroupBox(L"Chọn Từ Điển",BS_CENTER,r.left + 350,r.top + 15,385,80,hWnd,NULL,
				18,FW_BOLD,FALSE,FALSE,L"Tahoma",GetWindowInstance(hWnd));

			gEdit.createGroupBox(L"Biên Tập Từ Điển",BS_CENTER,r.left + 750,r.top + 15,250,80,hWnd,NULL,
				18,FW_BOLD,FALSE,FALSE,L"Tahoma",GetWindowInstance(hWnd));

			lList.createListBox(WS_CHILD|WS_VISIBLE|LBS_NOTIFY|WS_VSCROLL|LBS_SORT,r.left + 15,r.top + 135,r.left + 290,
				r.bottom - 150,hWnd,IDL_LISTWORD,15,FW_THIN,FALSE,FALSE,L"Tahoma",GetWindowInstance(hWnd));

			eMean.createRichEditBox(L"",WS_CHILD|WS_VISIBLE|ES_MULTILINE|
				ES_AUTOHSCROLL|ES_AUTOVSCROLL|ES_READONLY|WS_HSCROLL|WS_VSCROLL,r.left + 335,r.top + 135,r.right - 350,
				r.bottom - 158,hWnd,IDE_MEAN,15,FW_THIN,FALSE,FALSE,L"Tahoma",GetWindowInstance(hWnd));
			
			sInput.createStatic(L"Nhập từ:",  WS_CHILD|WS_VISIBLE|SS_LEFT, r.left + 15, r.top + 30,
				60,20,hWnd,NULL,15,FW_THIN,TRUE,FALSE,L"Tahoma",GetWindowInstance(NULL));

			cInput.createComboBox(L"",WS_CHILD|WS_VISIBLE|WS_BORDER|CBS_DROPDOWN|CBS_AUTOHSCROLL,r.left + 65,
				r.top + 25, 240, 500,hWnd,IDC_WORDINPUT,15,FW_THIN,FALSE,FALSE,L"Tahoma",GetWindowInstance(hWnd));
			
			bSearch.createButton(L"Tra Từ",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_TEXT,r.left + 65,r.top + 60,
				85,25,hWnd,IDB_SEARCH,15,FW_BLACK,FALSE,FALSE,L"Tahoma",GetWindowInstance(hWnd));

			bPronounce.createButton(L"Phát Âm",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_TEXT,r.left + 220,r.top + 60,
				85,25,hWnd,IDB_PRONOUNCE,15,FW_BLACK,FALSE,FALSE,L"Tahoma",GetWindowInstance(hWnd));

			cChoose.createComboBox(L"",WS_CHILD|WS_VISIBLE|WS_BORDER|BS_PUSHBUTTON|CBS_AUTOHSCROLL|CBS_DROPDOWNLIST|CBS_SORT,r.left + 355,
				r.top + 45, 375, 60,hWnd,IDC_CHOOSEDIC,15,FW_THIN,FALSE,FALSE,L"Tahoma",GetWindowInstance(hWnd));
			
			bEditW.createButton(L"Thêm/Xoá Từ",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_TEXT,r.left + 755,r.top + 35,
				100,25,hWnd,IDB_EDITW,15,FW_BLACK,FALSE,FALSE,L"Tahoma",GetWindowInstance(hWnd));

			bEditD.createButton(L"Thêm Từ Điển",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_TEXT,r.left + 755,r.top + 65,
				100,25,hWnd,IDB_EDITD,15,FW_BLACK,FALSE,FALSE,L"Tahoma",GetWindowInstance(hWnd));

			bPrint.createButton(L"Xuất Từ",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_TEXT,r.left + 895,r.top + 35,
				100,25,hWnd,IDB_PRINTWORD ,15,FW_BLACK,FALSE,FALSE,L"Tahoma",GetWindowInstance(hWnd));

			bHelp.createButton(L"Trợ Giúp",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_TEXT,r.left + 895,r.top + 65,
				100,25,hWnd,IDB_HELP,15,FW_BLACK,FALSE,FALSE,L"Tahoma",GetWindowInstance(hWnd));

			//init data of object

			//init List of Dictionary
			_bstr_t lod = dbTest.ListDict();
			RefreshDict(cChoose,lod);

			//init List of word
			_bstr_t low = mDict.SearchWord();
			RefreshList(lList,low);
			
			//init number of word
			TCHAR strDict[40], strType[60], bufer[60];
			DefineDict(cChoose,strDict,strType);
			TCHAR number[60];
			wcscpy(number, dbTest.CountWord(strDict,strType));
			int num = _wtoi(number);
			_stprintf(bufer,_T("Số lượng từ: %d"),num);
			CreateWindow(_T("STATIC"),bufer,WS_CHILD|WS_VISIBLE,390,70,250,17,hWnd,NULL,hInst,NULL);
			
		}break;
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDB_EDITW:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_DELADD), hWnd, DelAddProc);
			break;
		case IDB_EDITD:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ADDDICT), hWnd, AddDictProc);
			break;
		case IDB_HELP:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_HELP), hWnd, HelpProc);
			break;
		case IDB_PRINTWORD:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_PRINT), hWnd, PrintProc);
			break;
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;

		//List or word
		case IDL_LISTWORD:
			{
				switch(wmEvent)
				{
				case LBN_SELCHANGE:
					{
						TCHAR strDict[40], strType[60];
						DefineDict(cChoose,strDict,strType);
						
						LPTSTR textSelected = lList.getTextSelected();
						mMean.mean(eMean.getHandle(),textSelected,strDict,strType);
						cInput.add(textSelected);
						cInput.setText(textSelected);
					}
					break;
				}
			}
			break;

		//Loook up word
		case IDB_SEARCH:
			{
				
				TCHAR dictName[40];
				TCHAR dictType[60];
				DefineDict(cChoose,dictName,dictType);

				TCHAR buffer[20];
				GetWindowText(cInput.getHandle(),buffer,20);
				
				lList.searchList(buffer);
				LPTSTR textSelected = lList.getTextSelected();
				cInput.add(textSelected);
				cInput.setText(textSelected);
				mMean.mean(eMean.getHandle(),textSelected,dictName,dictType);
				
			}
			break;

		//Pronounce word
		case IDB_PRONOUNCE:
			{
				LPTSTR textSelected = lList.getTextSelected();
				mWord.setName(textSelected);
				mWord.LoadPronounce();
			}
			break;

		//Choose Dictionary
		case IDC_CHOOSEDIC:
			{
				switch(wmEvent)
				{
				case CBN_SELCHANGE:
					{
						TCHAR buffer[100];
						LPTSTR dictName;
						LPTSTR dictType;
						wcscpy(buffer,cChoose.getText());
						dictType = wcstok(buffer,L"(");
						dictName = wcstok(NULL,L")");

						mDict.SetDict(dictName,dictType);
						lList.clearList();
						if(mDict.CountWord()!=0)
						{
							LPTSTR asdf = mDict.SearchWord();
							RefreshList(lList,asdf);
						}
					}
					break;
				}
			}
			break;

		//Word Input
		case IDC_WORDINPUT:
			{
				switch(wmEvent)
				{
				case CBN_SELCHANGE:
					{
						TCHAR dictName[40];
						TCHAR dictType[60];
						DefineDict(cChoose,dictName,dictType);

						TCHAR buffer[20];
						GetWindowText(cInput.getHandle(),buffer,20);

						lList.searchList(buffer);
						LPTSTR textSelected = lList.getTextSelected();
						mMean.mean(eMean.getHandle(),textSelected, dictName,dictType);
					}
					break;
				case CBN_EDITCHANGE:
					{
						TCHAR buffer[20];
						GetWindowText(cInput.getHandle(),buffer,20);
						lList.searchList(buffer);
						LPTSTR textSelected = lList.getTextSelected();

					}
					break;
				}
			
			}
			break;
		
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		EndPaint(hWnd, &ps);
		break;
	case WM_SIZE:
		{
			gOption.move(r.left + 330,r.top,r.right - 340,100);
			gSearch.move(r.left + 10,r.top,r.left + 300,100);
			gList.move(r.left + 10,r.top + 110,r.left + 300,r.bottom - 120);
			gMean.move(r.left + 330,r.top + 110,r.right - 340,r.bottom - 120);
			lList.move(r.left + 15,r.top + 135,r.left + 290,r.bottom - 150);
			eMean.move(r.left + 335,r.top + 135,r.right - 350,r.bottom - 158);
		}break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}
INT_PTR CALLBACK AddDictProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{

	
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		switch (wmId)
		{
		
		case IDB_HELP:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_HELP), hDlg, HelpProc);
			break;
		case IDOK:
			{
				TCHAR strType[40],strName[60];
				GetDlgItemText(hDlg,IDC_PATH,strType,40);
				GetDlgItemText(hDlg,IDC_PATH2,strName,60);
				if(dbTest.AddDic(strType,strName))
				{
					//add dict successful

				}
				else
				{
					//can't not add
					MessageBox(hDlg,L"Không thể thêm được",L"Lỗi thêm từ điển",MB_ICONINFORMATION);
				}
			}
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		case IDCANCEL:
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}

INT_PTR CALLBACK DelAddProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	UNREFERENCED_PARAMETER(lParam);
	
	switch (message)
	{
	case WM_INITDIALOG:
		{
		//init dictionary list
			int i;
			int n = cChoose.count();
			LPTSTR buffer;
			TCHAR dest[100];
			buffer = cChoose.getItem(0);
			wcscpy(dest,buffer);
			SendDlgItemMessage(hDlg,IDC_COMBO1,CB_ADDSTRING,0,(LPARAM)dest);
			SendDlgItemMessage(hDlg,IDC_COMBO1,CB_SETCURSEL,0,0);

			//init word list
			LPWSTR dictName;
			LPWSTR dictType;
			dictType = wcstok(dest,L"(");
			dictName = wcstok(NULL,L")");
			mDict.SetDict(dictName,dictType);
			_bstr_t lod = mDict.SearchWord();
			
			LPTSTR aW;
			aW = wcstok(lod,L"\n");
			SendDlgItemMessage(hDlg,IDC_COMBO6,CB_ADDSTRING,0,(LPARAM)aW);
			SendDlgItemMessage(hDlg,IDC_COMBO8,CB_ADDSTRING,0,(LPARAM)aW);
			SendDlgItemMessage(hDlg,IDC_COMBO3,CB_ADDSTRING,0,(LPARAM)aW);
			do{
				aW = wcstok(NULL,L"\n");

				if (aW==NULL)break;
				SendDlgItemMessage(hDlg,IDC_COMBO6,CB_ADDSTRING,0,(LPARAM)aW);
				SendDlgItemMessage(hDlg,IDC_COMBO8,CB_ADDSTRING,0,(LPARAM)aW);
				SendDlgItemMessage(hDlg,IDC_COMBO3,CB_ADDSTRING,0,(LPARAM)aW);
			}while (aW!=NULL);

			//init dictionary list
			for(i = 1 ; i < n ; i++)
			{
				
				buffer = cChoose.getItem(i);
				wcscpy(dest,buffer);
				SendDlgItemMessage(hDlg,IDC_COMBO1,CB_ADDSTRING,0,(LPARAM)dest);
			}	

			//init type of word list
			SendDlgItemMessage(hDlg,IDC_COMBO2,CB_ADDSTRING,0,(LPARAM)L"Danh từ");
			SendDlgItemMessage(hDlg,IDC_COMBO2,CB_SETCURSEL,0,0);
			SendDlgItemMessage(hDlg,IDC_COMBO2,CB_ADDSTRING,0,(LPARAM)L"Động từ");
			SendDlgItemMessage(hDlg,IDC_COMBO2,CB_ADDSTRING,0,(LPARAM) L"Giới từ");
			SendDlgItemMessage(hDlg,IDC_COMBO2,CB_ADDSTRING,0,(LPARAM) L"Tính từ");
			SendDlgItemMessage(hDlg,IDC_COMBO2,CB_ADDSTRING,0,(LPARAM) L"Trạng từ");
			SendDlgItemMessage(hDlg,IDC_COMBO2,CB_ADDSTRING,0,(LPARAM) L"Ngoại động từ");
			SendDlgItemMessage(hDlg,IDC_COMBO2,CB_ADDSTRING,0,(LPARAM) L"Nội động từ");
			SendDlgItemMessage(hDlg,IDC_COMBO2,CB_ADDSTRING,0,(LPARAM)L"Viết tắt");
			SendDlgItemMessage(hDlg,IDC_COMBO2,CB_ADDSTRING,0,(LPARAM)	L"Không xác định");

			//init relative type
			SendDlgItemMessage(hDlg,IDC_COMBO7,CB_ADDSTRING,0,(LPARAM)L"Đồng nghĩa");
			SendDlgItemMessage(hDlg,IDC_COMBO7,CB_SETCURSEL,0,0);
			SendDlgItemMessage(hDlg,IDC_COMBO7,CB_ADDSTRING,0,(LPARAM)L"Trái nghĩa");
			SendDlgItemMessage(hDlg,IDC_COMBO7,CB_ADDSTRING,0,(LPARAM)L"Đồng âm");

			//contrast number of word
			SendDlgItemMessage(hDlg,IDC_EDIT1,EM_SETLIMITTEXT,20,0);
			SendDlgItemMessage(hDlg,IDC_EDIT5,EM_SETLIMITTEXT,20,0);
			SendDlgItemMessage(hDlg,IDC_EDIT2,EM_SETLIMITTEXT,255,0);
			SendDlgItemMessage(hDlg,IDC_EDIT4,EM_SETLIMITTEXT,255,0);


		}
		break;

	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		switch (wmId)
		{
		case IDB_HELP:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_HELP), hDlg, HelpProc);
			break;
		case IDCANCEL:
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		//Pick dicitonary
		case IDC_COMBO1:
			{
				switch(wmEvent)
				{
				case CBN_SELCHANGE:
					{
						TCHAR buffer[100];
						GetDlgItemText(hDlg,IDC_COMBO1,buffer,100);
						LPWSTR dictName;
						LPWSTR dictType;
						dictType = wcstok(buffer,L"(");
						dictName = wcstok(NULL,L")");
						mDict.SetDict(dictName,dictType);
						_bstr_t lod = mDict.SearchWord();

						//delete list or word in delete combo, add relative combo
						int nDelete = SendDlgItemMessage(hDlg,IDC_COMBO6,CB_GETCOUNT,0,0);
						int nRelative1 = SendDlgItemMessage(hDlg,IDC_COMBO8,CB_GETCOUNT,0,0);
						int nRelative2 = SendDlgItemMessage(hDlg,IDC_COMBO3,CB_GETCOUNT,0,0);
						int i;
						for (i = 0 ; i < nDelete; i++)
							SendDlgItemMessage(hDlg,IDC_COMBO6,CB_DELETESTRING,0,0);
						for (i = 0 ; i < nRelative1; i++)
							SendDlgItemMessage(hDlg,IDC_COMBO6,CB_DELETESTRING,0,0);
						for (i = 0 ; i < nRelative2; i++)
							SendDlgItemMessage(hDlg,IDC_COMBO6,CB_DELETESTRING,0,0);

		

						//update list or word in delete combo, add relative combo
						LPTSTR aW;
						aW = wcstok(lod,L"\n");

						SendDlgItemMessage(hDlg,IDC_COMBO6,CB_ADDSTRING,0,(LPARAM)aW);
						SendDlgItemMessage(hDlg,IDC_COMBO8,CB_ADDSTRING,0,(LPARAM)aW);
						SendDlgItemMessage(hDlg,IDC_COMBO3,CB_ADDSTRING,0,(LPARAM)aW);
						SetDlgItemText(hDlg,IDC_COMBO6,aW);
						
						do{
							aW = wcstok(NULL,L"\n");

							if (aW==NULL)break;
							SendDlgItemMessage(hDlg,IDC_COMBO6,CB_ADDSTRING,0,(LPARAM)aW);
							SendDlgItemMessage(hDlg,IDC_COMBO8,CB_ADDSTRING,0,(LPARAM)aW);
							SendDlgItemMessage(hDlg,IDC_COMBO3,CB_ADDSTRING,0,(LPARAM)aW);
						}while (aW!=NULL);
					}
					break;
				}
	
			}
			break;
		//pick word to delete
		case IDC_COMBO6:
			{
				switch(wmEvent)
				{
				case CBN_EDITUPDATE:
					{
						SendDlgItemMessage(hDlg,IDC_COMBO6,CB_SHOWDROPDOWN,TRUE,0);
					}
					break;
				}
			}
			break;
		//delete word
		case IDC_BUTTON1:
			{
				TCHAR wordToBeDestroyed[20];
				GetDlgItemText(hDlg,IDC_COMBO6,wordToBeDestroyed,20);
				if(!dbTest.DeleteWord(wordToBeDestroyed))
				{
					TCHAR err[100];
					_stprintf(err,_T("Từ \"%s\" không có trong từ điển, xóa không thành công!"),wordToBeDestroyed);
					MessageBox(hDlg,err,L"Lỗi xóa từ",MB_ICONINFORMATION);
				}
			}
			break;
		//pick word to edit 
		case IDC_COMBO8:
			{
				TCHAR strChose[20];
				GetDlgItemText(hDlg,IDC_COMBO8,strChose,20);
				int index = SendDlgItemMessage(hDlg,IDC_COMBO3,CB_FINDSTRINGEXACT,0,(LPARAM)strChose);
				SendDlgItemMessage(hDlg,IDC_COMBO3,CB_DELETESTRING,index,0);
			}
			break;
		// add mean , relative word
		case IDC_BUTTON4:
			{
				TCHAR strChose[20], strRelative1[20], strRelative2[20], strMean[255], strExample[255], strType[3];
				wcscpy(strChose,L"");
				wcscpy(strRelative1,L"");
				wcscpy(strRelative2,L"");
				wcscpy(strMean,L"");
				wcscpy(strExample,L"");
				wcscpy(strType,L"");
					
				if(strChose==NULL) MessageBox(0,0,0,0);
				GetDlgItemText(hDlg,IDC_COMBO8,strChose,20);
				GetDlgItemText(hDlg,IDC_COMBO3,strRelative1,20);
				GetDlgItemText(hDlg,IDC_COMBO7,strRelative2,20);
				GetDlgItemText(hDlg,IDC_EDIT4,strMean,20);
				GetDlgItemText(hDlg,IDC_EDIT2,strExample,20);
				GetDlgItemText(hDlg,IDC_COMBO2,strType,3);


				if(wcslen(strChose)!=0)
				{
					TCHAR buffer[100];
					GetDlgItemText(hDlg,IDC_COMBO1,buffer,100);
					LPWSTR dictName;
					LPWSTR dictType;
					dictType = wcstok(buffer,L"(");
					dictName = wcstok(NULL,L")");
					mDict.SetDict(dictName,dictType);

					if(wcscmp(strRelative2,L"Đồng nghĩa")==0)
						wcscpy(strRelative2,L"sm");
					else if(wcscmp(strRelative2,L"Trái nghĩa")==0)
						wcscpy(strRelative2,L"om");
					else if(wcscmp(strRelative2,L"Đồng âm")==0)
						wcscpy(strRelative2,L"sp");

					if(wcslen(strRelative1)!= 0)
						dbTest.AddRefWord(strChose,strRelative1,strRelative2);

					if(wcscmp(strRelative2,L"Đồng nghĩa")==0)
						wcscpy(strRelative2,L"sm");
					else if(wcscmp(strRelative2,L"Trái nghĩa")==0)
						wcscpy(strRelative2,L"om");
					else if(wcscmp(strRelative2,L"Đồng âm")==0)
						wcscpy(strRelative2,L"sp");

 					if(wcslen(strMean)!= 0)
 						dbTest.AddMeanOnly(strChose,strMean,strType,strExample,dictName,dictType);
				}

			}
			break;
		//add word
		case IDC_BUTTON3:
			{
				//specify dictionary
				TCHAR strDict[100];
				GetDlgItemText(hDlg,IDC_COMBO1,strDict,20);
				LPWSTR dictName;
				LPWSTR dictType;
				dictType = wcstok(strDict,L"(");
				dictName = wcstok(NULL,L")");
				mDict.SetDict(dictName,dictType);
				
				TCHAR strWord[20],strProu[20];
				GetDlgItemText(hDlg,IDC_EDIT1,strWord,20);
				GetDlgItemText(hDlg,IDC_EDIT1,strProu,20);
				
				dbTest.AddWordOnly(strProu,strWord,dictName,dictType); // flag = 0:add word
			}
			break;
		}
		break;
	}
	return (INT_PTR)FALSE;
}
INT_PTR CALLBACK PrintProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		{
			int i;
			int n = lList.count();
			
			
			for(i = 0 ; i < n; i++)
			{
				
				TCHAR buf[21];
				wcscpy(buf,lList.getText(i));
				SendDlgItemMessage(hDlg,IDC_LIST1,LB_ADDSTRING,0,(LPARAM)buf);
			}
		
		}
		break;
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		switch (wmId)
		{
		case IDC_LIST2:
			{
				switch(wmEvent)
				{
				case LBN_SELCHANGE:
					{
						LPTSTR str = new WCHAR[21];
						int index = SendDlgItemMessage(hDlg,IDC_LIST2,LB_GETCURSEL,0,0);
						SendDlgItemMessage(hDlg,IDC_LIST2,LB_GETTEXT,index,(LPARAM)str);
						HWND hRich = GetDlgItem(hDlg,IDC_RICHEDIT21);
					
						TCHAR strDict[40], strType[60];
						DefineDict(cChoose,strDict,strType);
						mMean.mean(hRich,str,strDict,strType); 
					}
					break;
				}
			}
			break;
		//them
		case IDC_BUTTON1:
			{
				LPTSTR str = new WCHAR[21];
				int index = SendDlgItemMessage(hDlg,IDC_LIST1,LB_GETCURSEL,0,0);
				SendDlgItemMessage(hDlg,IDC_LIST1,LB_GETTEXT,index,(LPARAM)str);
				int iExist = SendDlgItemMessage(hDlg,IDC_LIST2,LB_FINDSTRINGEXACT,-1,(LPARAM)str);
				if(iExist<0)
				{
					SendDlgItemMessage(hDlg,IDC_LIST2,LB_ADDSTRING,0,(LPARAM)str);
					HWND hRich = GetDlgItem(hDlg,IDC_RICHEDIT21);
				
 					TCHAR strDict[40], strType[60];
 					DefineDict(cChoose,strDict,strType);
 					mMean.mean(hRich,str,strDict,strType); 
				}
			}
			break;
		//xoa

		case IDC_BUTTON7:
			{
				LPTSTR str = new WCHAR[21];
				int index = SendDlgItemMessage(hDlg,IDC_LIST2,LB_GETCURSEL,0,0);
				SendDlgItemMessage(hDlg,IDC_LIST2,LB_DELETESTRING,index,0);
			}
			break;
			
		case IDC_BUTTON8:
			{
 				TCHAR fileName[256];
				TCHAR  word[21];
				
 				char *outChar;

				// check file name of output file
				wcscmp(fileName,L"");
				GetDlgItemText(hDlg,IDC_EDIT1,fileName,256);
				wcscat(fileName,L".doc");
				if(wcscmp(fileName,L".doc")==0)
				{
					//fileName not exists
					MessageBox(hDlg,L"Hãy nhập tên file vào ô \"Tên file :\" ",L"Chưa có tên file",MB_ICONWARNING);
					break;
				}
				
				HANDLE hFile = CreateFile(fileName,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
				if(hFile== INVALID_HANDLE_VALUE)
				{
					MessageBox(hDlg,L"Hãy nhập tên file khác vào ô \"Tên file :\" ",L"Tên tập tin đã tồn tại",MB_ICONWARNING);
					break;
				}
				// check list word to print
				int n  = SendDlgItemMessage(hDlg,IDC_LIST2,LB_GETCOUNT,0,0);
				for (int i = 0 ; i < n ; i++)
				{
					TCHAR bp[200], obuffer[500];
					LPTSTR buffer;
					TCHAR dictName[40];
					TCHAR dictType[60];
					SendDlgItemMessage(hDlg,IDC_LIST2,LB_GETTEXT,i,(LPARAM)word);
					DefineDict(cChoose,dictName,dictType);
					wcscpy(obuffer,word);
					wcscat(obuffer,L"\n");
					buffer = dbTest.GetMeanOfWord(word,dictName,dictType);
			
					DWORD dwBytesWritten = 0;
					WriteFile(hFile,(LPVOID)obuffer,wcslen(obuffer)*2,&dwBytesWritten,NULL);
					WriteFile(hFile,(LPVOID)buffer,wcslen(buffer)*2,&dwBytesWritten,NULL);
					wcscpy(bp,L"----------------------------------------------------------------------\n\n");
					WriteFile(hFile,(LPVOID)bp,wcslen(bp)*2,&dwBytesWritten,NULL);
				}
				int resul = MessageBox(hDlg,L"Đã in thành công ra tập tin word, bạn có muốn mở tập tin này không?" ,L"In thành công",MB_ICONASTERISK|MB_YESNO);
				if(resul == IDYES)
					ShellExecute(NULL,L"open",fileName,NULL,NULL,SW_SHOWNORMAL);
				CloseHandle(hFile);
			}
			break;
		//in
		case IDB_HELP:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_HELP), hDlg, HelpProc);
			break;
		case IDCANCEL:
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}
INT_PTR CALLBACK HelpProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	
	case WM_INITDIALOG:
		{
			CreateWindow(L"LISTBOX",NULL,WS_CHILD,0,0,0,0,hDlg,NULL,GetWindowInstance(hDlg),NULL);
			HWND hListAct = GetDlgItem(hDlg,IDC_LIST1);
			mHelp.Surf("help.txt",hListAct);
		}
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		int wmId, wmEvent;
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		switch (wmId)
		{
		case IDC_LIST1:
			{
				switch(wmEvent)
				{
				case LBN_SELCHANGE:
					{
						TCHAR buffer[30];
						int index = SendDlgItemMessage(hDlg,IDC_LIST1,LB_GETCURSEL,0,0);
						SendDlgItemMessage(hDlg,IDC_LIST1,LB_GETTEXT,index,(LPARAM)buffer);
						HWND hEdit = GetDlgItem(hDlg,IDC_EDIT2);
						SetWindowText(hEdit,L"");
						mHelp.LoadHelp("help.txt",buffer,hEdit);
					}
					break;
				}
			}
			break;
		case IDOK:
			{
				EndDialog(hDlg, LOWORD(wParam));
				return (INT_PTR)TRUE;
			}break;
		case IDCANCEL:
			{
				EndDialog(hDlg, LOWORD(wParam));
				return (INT_PTR)TRUE;
			}break;
		}
		
		
		
	}
	return (INT_PTR)FALSE;
}
